package com.ruoyi.mateProcurement.service.impl;

import com.ruoyi.audit.domain.Audit;
import com.ruoyi.audit.mapper.AuditMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.mateProcurement.domain.MateOutbound;
import com.ruoyi.mateProcurement.domain.MateOutboundModel;
import com.ruoyi.mateProcurement.mapper.MateOutboundMapper;
import com.ruoyi.mateProcurement.service.MateOutboundService;
import com.ruoyi.mateWarehousing.domain.MateWarehousing;
import com.ruoyi.mateWarehousing.mapper.MateWarehousingMapper;
import com.ruoyi.warehousing.domain.WarehousingRecords;
import com.ruoyi.warehousing.mapper.WarehousingRecordsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class MateOutboundServiceImpl implements MateOutboundService {
    @Autowired
    MateOutboundMapper mateOutboundMapper;
    @Autowired
    AuditMapper auditMapper;

    @Autowired
    MateWarehousingMapper warehousingMapper;

    @Autowired
    WarehousingRecordsMapper warehousingRecordsMapper;

    @Override
    public int insertMateOutbound(MateOutbound mateOutbound) {
        mateOutbound.setDeptId(SecurityUtils.getLoginUser().getDeptId());
        int i = mateOutboundMapper.insertMateOutbound(mateOutbound);
        if (mateOutbound.getMateOutboundModels() != null) {
            try {
                for (MateOutboundModel mateOutboundModel : mateOutbound.getMateOutboundModels()) {
                    mateOutboundModel.setDeptId(SecurityUtils.getLoginUser().getDeptId());
                    mateOutboundModel.setMateGoodsId(mateOutbound.getMateGoodsId());
                    mateOutboundMapper.insertMateOutboundModel(mateOutboundModel);
                }
            } catch (Exception e) {
                mateOutboundMapper.deleteMateOutboundById(mateOutbound.getMateGoodsId());
            }
        }

        Audit audit = new Audit();
        audit.setOrderId(mateOutbound.getMateGoodsId());
        audit.setCompanyId(SecurityUtils.getLoginUser().getCompanyId());
        audit.setOrderType("9");
        audit.setAuditStatus("0");
        audit.setUserId(mateOutbound.getUserId());
        audit.setSupplierId(mateOutbound.getMaterialId());
        audit.setTaxAmount(mateOutbound.getTaxAmount());
        audit.setOrderNum(mateOutbound.getMateGoodsNum());
        auditMapper.insertAudit(audit);
        return i;

    }

    @Override
    public MateOutbound selectMateOutbound(Map<String, Object> resMap) {
        return mateOutboundMapper.selectMateOutbound(resMap);
    }

    @Override
    public List<MateOutbound> selectMateOutboundList(MateOutbound mateOutbound) {
        return mateOutboundMapper.selectMateOutboundList(mateOutbound);
    }

    @Override
    public MateOutbound selectMateOutboundBy(MateOutbound mateOutbound) {
        return mateOutboundMapper.selectMateOutboundBy(mateOutbound);
    }

    @Override
    public List<MateOutboundModel> selectMateOutboundModelBy(Long mateGoodsId) {
        return mateOutboundMapper.selectMateOutboundModelBy(mateGoodsId);
    }

    @Override
    public void deleteMateOutbound(Long mateGoodsId) {
        HashMap hashMap = new HashMap();
        hashMap.put("orderId", mateGoodsId);
        hashMap.put("orderType", 9);
        auditMapper.deleteAuditWith(hashMap);
        mateOutboundMapper.deleteMateOutboundById(mateGoodsId);
        mateOutboundMapper.deleteMateOutboundModelById(mateGoodsId);
    }

    @Override
    public int updateMateOutbound(MateOutbound mateOutbound) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long mateGoodsId = mateOutbound.getMateGoodsId();
        List<Long> longs = mateOutboundMapper.selectMateOutboundWarehousingIdlist(mateGoodsId);
        List<Long> longs1 = new ArrayList<>();
        Audit audit = new Audit();
        audit.setOrderId(mateOutbound.getMateGoodsId());
        audit.setCompanyId(sysUser.getCompanyId());
        audit.setOrderType("9");
        audit.setAuditStatus("0");
        audit.setUserId(mateOutbound.getUserId());
        audit.setSupplierId(mateOutbound.getMaterialId());
        audit.setTaxAmount(mateOutbound.getTaxAmount());
        auditMapper.updateAuditOther(audit);
        if (mateOutbound.getMateOutboundModels() != null) {
            for (int i = 0; i < mateOutbound.getMateOutboundModels().size(); i++) {
                mateOutbound.getMateOutboundModels().get(i).setMateGoodsModelId(null);
                mateOutbound.getMateOutboundModels().get(i).setMateGoodsId(mateOutbound.getMateGoodsId());
            }
            for (int i = 0; i < longs.size(); i++) {
                longs1.add(longs.get(i));
            }
            for (MateOutboundModel mateOutboundModel : mateOutbound.getMateOutboundModels()) {
                if (mateOutboundModel.getMateGoodsModelId() != null) {
                    Long mateGoodsModelId = mateOutboundModel.getMateGoodsModelId();
                    longs1.add(mateGoodsModelId);
                    mateOutboundMapper.updateMateOutboundModel(mateOutboundModel);
                } else {
                    mateOutboundModel.setMateGoodsId(mateOutbound.getMateGoodsId());
                    mateOutboundModel.setDeptId(sysUser.getCompanyId());
                    mateOutboundMapper.insertMateOutboundModel(mateOutboundModel);
                }
            }
            if (longs1.size() != 0 && longs != null) {
                for (int i = 0; i < longs.size(); i++) {
                    mateOutboundMapper.deleteMateOutboundModelById(longs.get(i));
                }
            }
        } else {
            if (longs != null) {
                for (int i = 0; i < longs.size(); i++) {
                    mateOutboundMapper.deleteMateOutboundModelById(longs.get(i));
                }
            }
        }
        int num = mateOutboundMapper.updateMateOutbound(mateOutbound);
        return num;
    }

    @Override
    public int caculation(MateOutbound mateOutbound) {
        SysUser sysUser = new SysUser();
        MateOutbound mateOutbound1 = mateOutboundMapper.selectMateOutboundBy(mateOutbound);
        WarehousingRecords warehousingRecords = new WarehousingRecords();
        if (mateOutbound1.getMateOutboundModels() != null) {
            List<MateOutboundModel> mateOutboundModels = mateOutbound1.getMateOutboundModels();
            for (MateOutboundModel mateOutboundModel : mateOutboundModels) {
                Long deptId = sysUser.getDeptId();
                String productType = mateOutboundModel.getMateProductType();
                Long productId = mateOutboundModel.getMateproductId();
                String batch = mateOutboundModel.getMateBatch();
                int num = mateOutboundModel.getNum();
                MateWarehousing mateWarehousing = new MateWarehousing();
                mateWarehousing.setDeptId(deptId);
                mateWarehousing.setMateProductType(productType);
                mateWarehousing.setMateproductId(productId);
                mateWarehousing.setMateBatch(batch);
                MateWarehousing mateWarehousing1 = warehousingMapper.selectMateWarehousing(mateWarehousing);
                warehousingRecords.setInventoryNum(-mateOutboundModel.getNum());
                warehousingRecords.setAllnum(-mateOutboundModel.getNum());
                if (mateWarehousing1 == null) {
                    warehousingMapper.insertMateWarehousing(mateWarehousing);
                } else {
                    int warehousingNum = mateWarehousing1.getWarehousingNum();
                    warehousingNum = warehousingNum - num;
                    mateWarehousing.setWarehousingNum(warehousingNum);
                    mateWarehousing.setWarehousingId(mateWarehousing1.getWarehousingId());
                    warehousingMapper.updateMateWarehousing(mateWarehousing);
                }
                Long warehousingId = mateWarehousing.getWarehousingId();
                warehousingRecords.setOrderNum(mateOutbound1.getMateGoodsNum());
                warehousingRecords.setWarehousingId(warehousingId);
                warehousingRecords.setSupplierId(mateOutbound1.getMaterialId());
                warehousingRecords.setInventoryStatus("9");
                warehousingRecords.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
                warehousingRecordsMapper.insertWarehousingRecords(warehousingRecords);
            }

        }
        return 1;
    }

    @Override
    public List<MateWarehousing> selectSaleWarehousing(Map<String, Object> resMap) {
        return warehousingMapper.selectSaleWarehousing(resMap);
    }
}
